'use strict';

const {
	create
} = require("domain");

const db = uniCloud.database()
const dbCmd = db.command;
exports.main = async (event, context) => {
	const {
		userId,
		articleId,
		content
	} = event;
	//获取用户，文章信息，找到指定文章，追加评论
	let user = await db.collection('user').doc(userId).get();
	console.log(user)
	user = user.data[0];
	// let article = await db.collection('article').doc(articleId).get();

	const generateId = (number) => {
		return Number(Math.random().toString().substr(3, number) + Date.now()).toString(36);
	};
	let commentObj = {
		comment_id: generateId(5),
		comment_content: content,
		create_time: Date.now(),
		is_reply: false,
		replyArr: [],
		author: {
			author_id: user._id,
			author_name: user.author_name,
			avatar: user.avatar,
			professional: user.professional
		}
	};
	commentObj = dbCmd.unshift(commentObj)
	await db.collection('article').doc(articleId).update({
		comments: commentObj
	})

	return {
		code: 0,
		data: {
			msg: '提交成功'
		}
	}

};